﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using TCS.DBUtility;//请先添加引用
namespace TCS.DAL
{
    /// <summary>
    /// 数据访问类Gsp_DrugsInfo。
    /// </summary>
    public class Gsp_DrugsInfo
    {
        public Gsp_DrugsInfo()
        { }
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("DrugID", "Gsp_DrugsInfo");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int DrugID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Gsp_DrugsInfo");
            strSql.Append(" where DrugID=@DrugID ");
            SqlParameter[] parameters = {
					new SqlParameter("@DrugID", SqlDbType.Int,4)};
            parameters[0].Value = DrugID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(TCS.Model.Gsp_DrugsInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Gsp_DrugsInfo(");
            strSql.Append("MemberID,ProductName,ComName,ApprovalNumber,Formulations,Specifications,ProductionUnit)");
            strSql.Append(" values (");
            strSql.Append("@MemberID,@ProductName,@ComName,@ApprovalNumber,@Formulations,@Specifications,@ProductionUnit)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@MemberID", SqlDbType.Int,4),
					new SqlParameter("@ProductName", SqlDbType.NVarChar,50),
					new SqlParameter("@ComName", SqlDbType.NVarChar,50),
					new SqlParameter("@ApprovalNumber", SqlDbType.NVarChar,50),
					new SqlParameter("@Formulations", SqlDbType.NVarChar,50),
					new SqlParameter("@Specifications", SqlDbType.NVarChar,50),
					new SqlParameter("@ProductionUnit", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.MemberID;
            parameters[1].Value = model.ProductName;
            parameters[2].Value = model.ComName;
            parameters[3].Value = model.ApprovalNumber;
            parameters[4].Value = model.Formulations;
            parameters[5].Value = model.Specifications;
            parameters[6].Value = model.ProductionUnit;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(TCS.Model.Gsp_DrugsInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Gsp_DrugsInfo set ");
            strSql.Append("MemberID=@MemberID,");
            strSql.Append("ProductName=@ProductName,");
            strSql.Append("ComName=@ComName,");
            strSql.Append("ApprovalNumber=@ApprovalNumber,");
            strSql.Append("Formulations=@Formulations,");
            strSql.Append("Specifications=@Specifications,");
            strSql.Append("ProductionUnit=@ProductionUnit");
            strSql.Append(" where DrugID=@DrugID ");
            SqlParameter[] parameters = {
					new SqlParameter("@DrugID", SqlDbType.Int,4),
					new SqlParameter("@MemberID", SqlDbType.Int,4),
					new SqlParameter("@ProductName", SqlDbType.NVarChar,50),
					new SqlParameter("@ComName", SqlDbType.NVarChar,50),
					new SqlParameter("@ApprovalNumber", SqlDbType.NVarChar,50),
					new SqlParameter("@Formulations", SqlDbType.NVarChar,50),
					new SqlParameter("@Specifications", SqlDbType.NVarChar,50),
					new SqlParameter("@ProductionUnit", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.DrugID;
            parameters[1].Value = model.MemberID;
            parameters[2].Value = model.ProductName;
            parameters[3].Value = model.ComName;
            parameters[4].Value = model.ApprovalNumber;
            parameters[5].Value = model.Formulations;
            parameters[6].Value = model.Specifications;
            parameters[7].Value = model.ProductionUnit;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int DrugID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Gsp_DrugsInfo ");
            strSql.Append(" where DrugID=@DrugID ");
            SqlParameter[] parameters = {
					new SqlParameter("@DrugID", SqlDbType.Int,4)};
            parameters[0].Value = DrugID;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TCS.Model.Gsp_DrugsInfo GetModel(int DrugID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 DrugID,MemberID,ProductName,ComName,ApprovalNumber,Formulations,Specifications,ProductionUnit from Gsp_DrugsInfo ");
            strSql.Append(" where DrugID=@DrugID ");
            SqlParameter[] parameters = {
					new SqlParameter("@DrugID", SqlDbType.Int,4)};
            parameters[0].Value = DrugID;

            TCS.Model.Gsp_DrugsInfo model = new TCS.Model.Gsp_DrugsInfo();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["DrugID"].ToString() != "")
                {
                    model.DrugID = int.Parse(ds.Tables[0].Rows[0]["DrugID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MemberID"].ToString() != "")
                {
                    model.MemberID = int.Parse(ds.Tables[0].Rows[0]["MemberID"].ToString());
                }
                model.ProductName = ds.Tables[0].Rows[0]["ProductName"].ToString();
                model.ComName = ds.Tables[0].Rows[0]["ComName"].ToString();
                model.ApprovalNumber = ds.Tables[0].Rows[0]["ApprovalNumber"].ToString();
                model.Formulations = ds.Tables[0].Rows[0]["Formulations"].ToString();
                model.Specifications = ds.Tables[0].Rows[0]["Specifications"].ToString();
                model.ProductionUnit = ds.Tables[0].Rows[0]["ProductionUnit"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select DrugID,MemberID,ProductName,ComName,ApprovalNumber,Formulations,Specifications,ProductionUnit ");
            strSql.Append(" FROM Gsp_DrugsInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" DrugID,MemberID,ProductName,ComName,ApprovalNumber,Formulations,Specifications,ProductionUnit ");
            strSql.Append(" FROM Gsp_DrugsInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Gsp_DrugsInfo";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}

